﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace NF_Tools
{

    public partial class BF_Button : Button
    {
        public BF_Button()
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;       //设置按钮默认样式
            this.BackColor = Color.FromArgb(59, 180, 242);              //设置按钮默认背景颜色
            this.FlatAppearance.BorderSize = 0;                         //设置边框默认边框值
            this.ForeColor = Color.White;                               //设置字体颜色
            this.Size = new System.Drawing.Size(76, 33);                //设置默认按钮大小
            //SetStyle();
            InitializeComponent();
        }


        /// <summary>
        /// 声明自绘，优化闪烁
        /// </summary>
        private void SetStyle()
        {
            base.SetStyle(
                ControlStyles.UserPaint |                               //用户自定义绘制
                ControlStyles.OptimizedDoubleBuffer |                   //设置双缓冲减少闪烁
                ControlStyles.AllPaintingInWmPaint |                    //空间忽略windows消息减少闪烁
                ControlStyles.ResizeRedraw |                            //调整大小重绘
                ControlStyles.SupportsTransparentBackColor, true);      //模拟透明
            base.UpdateStyles();
        }


        /// <summary>
        /// 得到焦点时按钮背景色
        /// </summary>
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            base.BackColor = HoverColor;
            base.ForeColor = HoverTextColor;
        }

        /// <summary>
        /// 失去焦点时按钮背景及字体色
        /// </summary>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            base.BackColor = LeaveColor;
            base.ForeColor = LeaveTextColor;
        }

        /// <summary>
        /// 绘制边框
        /// </summary>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            if (!this.IsDisposed)
            {
                Pen p = new Pen(BorderColor);
                Graphics grh = this.CreateGraphics();
                grh.SmoothingMode = SmoothingMode.HighQuality;
                grh.PixelOffsetMode = PixelOffsetMode.HighQuality;
                grh.DrawRectangle(p, 0, 0, this.Width, this.Height);
            }
        }



        #region - 自定义属性 -

        private Color _HoverColor = Color.FromArgb(14, 144, 210);
        private Color _LeaveColor = Color.FromArgb(59, 180, 242);
        private Color _BorderColor = Color.FromArgb(59, 180, 242);
        private Color _HoverTextColor = Color.Wheat;
        private Color _LeaveTextColor = Color.Black;


        [Browsable(true), Category("自定义属性"), Description("文字颜色(得到焦点时)")]
        public Color HoverTextColor
        {
            get { return _HoverTextColor; }
            set { _HoverTextColor = value; }
        }

        [Browsable(true), Category("自定义属性"), Description("文字颜色(失去焦点时)")]
        public Color LeaveTextColor
        {
            get { return _LeaveTextColor; }
            set { _LeaveTextColor = value; }
        }

        [Browsable(true), Category("自定义属性"), Description("按钮颜色(得到焦点时)")]
        public Color HoverColor
        {
            get { return _HoverColor; }
            set { _HoverColor = value; }
        }

        [Browsable(true), Category("自定义属性"), Description("按钮颜色(失去焦点时)")]
        public Color LeaveColor
        {
            get { return _LeaveColor; }
            set { _LeaveColor = value; }
        }

        [Browsable(true), Category("自定义属性"), Description("边框颜色")]
        public Color BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; }
        }


        #endregion

    }
}
